package com.stars.empoly.activity

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.core.net.toUri
import androidx.lifecycle.lifecycleScope
import com.baidu.mapapi.search.core.PoiInfo
import com.eazypermissions.common.model.PermissionResult
import com.eazypermissions.coroutinespermission.PermissionManager
import com.google.gson.Gson
import com.luck.picture.lib.PictureSelector
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.empoly.R
import com.stars.empoly.base.MBActivity
import com.stars.empoly.config.MIntentAction
import com.stars.empoly.databinding.ActivityCooperationBinding
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.inte.OnUploadCallback
import com.stars.empoly.model.bean.UpImgBean
import com.stars.empoly.utils.ChosePhotoUtils
import com.stars.empoly.utils.FileUtils
import com.stars.empoly.utils.OssUtils
import com.stars.empoly.utils.PopuListUtils
import com.stars.empoly.vmodel.CooperationVModel
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch
import java.io.File
import java.util.ArrayList

/**
 *on 2022/9/24
 */
class CooperationActivity : MBActivity<CooperationVModel, ActivityCooperationBinding>(),
    OnUploadCallback<UpImgBean> {

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        //获取认证状态
        vm.getCoopStatus()

        vm.bean.observe(this){
            if(null!=it){
                if(it.status==0){
                    toast("信息审核中!")
                }
                if(it.status==1){
                    toast("信息已通过！")
                }
                if(it.status==-1){
                    toast("信息已驳回！")
                }
            }
        }

        vm.cityList.observe(this){
            if(it.isEmpty()){
                return@observe
            }
            if(null!=it&& it!!.isNotEmpty()){

                var list= java.util.ArrayList<String>()

                for(item in it){
                    list.add(item.name)
                }
                PopuListUtils.showQMUIPopup(context(),binding.llCity,list,object : ChoiceData {
                    override fun onRestuse(context: String?) {
                        for(item in vm.cityList.value!!){
                            if(item.name == context){
                                vm.cityId=item.id
                                binding.tvCity.text=item.name

                            }
                        }

                    }

                })


            }



        }

    }

    override fun listeners() {
        super.listeners()
        binding.llMale.onClick {
            vm.sex="1"
            binding.imgMale.setImageResource(R.drawable.me_he_on)
            binding.imgGirl.setImageResource(R.drawable.me_he_off)

        }
        binding.llGirl.onClick {
            vm.sex="0"
            binding.imgMale.setImageResource(R.drawable.me_he_off)
            binding.imgGirl.setImageResource(R.drawable.me_he_on)
        }
        binding.llCity.onClick {
            vm.getCityList()
        }
        binding.flImg.onClick {
            //选择照片

            lifecycleScope.launch {
                if (PermissionManager.requestPermissions(
                        this@CooperationActivity, 1000,
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    ) is PermissionResult.PermissionGranted
                ) {
                    ChosePhotoUtils.getChosePhoto(view()!!.context(), 1)

                }

            }

        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }


        if (requestCode == MIntentAction.REQUEST_CODE_IMG) {//图片

            val selectList = PictureSelector.obtainMultipleResult(data)
            val list = ArrayList<String>()
            if (null != selectList) {
                for (item in selectList) {

                    if (item.path.contains("content://")) {

                        val cr = this.contentResolver
                        list.add(FileUtils.getFilePathFromContentUri(item.path.toUri(), cr))
                    } else {
                        list.add(item.path)
                    }

//                    //添加一个默认
//                    val bugAttachment = GoodsImgBean(0, "", 0, true)
//                    recordAdapter!!.addData(recordAdapter!!.data.size - 1, bugAttachment)
//                    vm.nopath.add("")
                }


                OssUtils(this, vm)
                    .setUpLoad("avatar", list, this)

            }


            return

        }
        if (requestCode == MIntentAction.REQUEST_CODE_CAMERA) {//拍照
            //拍照
            val selectList = PictureSelector.obtainMultipleResult(data) ?: return
            val list = ArrayList<String>()
            if (selectList[0].path.contains("content://")) {

                val cr = this.contentResolver
                list.add(FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(), cr))

            } else {
                list.add(selectList[0].path)
            }

//            binding.imgAvater.load(File(list[0]))

            // vm.headImg.value=list[0]
            OssUtils(this, vm)
                .setUpLoad("avatar", list, this)

            return
        }


    }



    override fun vmClass(): Class<CooperationVModel> {
        return CooperationVModel::class.java
    }
    override fun binding(): ActivityCooperationBinding {
       return ActivityCooperationBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return "商户合作"
    }

    override fun onUploadSuccess(t: UpImgBean) {
        vm.lifePhoto.value = t.url


    }

    override fun onUploadError(error: String?) {
    }
}
